package com.qc.common.base;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.qc.common.config.Global;
import com.qc.common.utils.IdGen;
import com.qc.sys.entity.User;
import com.qc.sys.utils.UserUtils;
import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
import org.springframework.format.annotation.DateTimeFormat;

import java.io.Serializable;
import java.util.Date;

/**
 * 基础实体类
 */
public abstract class BaseEntity<T> implements Serializable {

    protected String id;
    @DateTimeFormat(iso = DateTimeFormat.ISO.DATE)
    protected Date createDate;
    @DateTimeFormat(iso = DateTimeFormat.ISO.DATE)
    protected Date updateDate;
    protected String createId;
    protected String updateId;
    protected Integer page = 1;

    protected Integer limit = 10;


    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public Integer getPage() {
        return page;
    }

    public void setPage(Integer page) {
        this.page = page;
    }

    public Integer getLimit() {
        return limit;
    }

    public void setLimit(Integer limit) {
        this.limit = limit;
    }

    public Date getCreateDate() {
        return createDate;
    }

    public void setCreateDate(Date createDate) {
        this.createDate = createDate;
    }

    public Date getUpdateDate() {
        return updateDate;
    }

    public void setUpdateDate(Date updateDate) {
        this.updateDate = updateDate;
    }

    public void preInsert() {
        // 不限制ID为UUID，调用setIsNewRecord()使用自定义ID
        this.id = IdGen.uuid();
        this.updateDate = new Date();
        if(this.createDate==null) {
            this.createDate = this.updateDate;
        }
    }

    /**
     * 更新之前执行方法，需要手动调用
     */
    public void preUpdate() {
        this.updateDate = new Date();
    }


    public String getCreateId() {
        return createId;
    }

    public void setCreateId(String createId) {
        this.createId = createId;
    }

    public String getUpdateId() {
        return updateId;
    }

    public void setUpdateId(String updateId) {
        this.updateId = updateId;
    }

    @Override
    public String toString() {
        return ReflectionToStringBuilder.toString(this);
    }

    /**
     * 获取数据库名称
     */
    @JsonIgnore
    public String getDbName(){
        return Global.getConfig("jdbc.type");
    }
}
